<?php

class AdminKhuyenmai extends CActiveRecord{
    
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'khuyenmai';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}

    public function getAllRowsByKhuyenmai(){        
        $row = array();
        $sql = "SELECT * FROM `khuyenmai`";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row;
    }
    
    public function getRowsByKhuyenmaiPaging($begin,$end){        
        $row = array();        
        $sql = "SELECT * FROM `khuyenmai` LIMIT ".$begin.",".$end;            
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();            
        return $row;
    }
    
    public function getRowsById($id){

        $row = array();        
        $sql = "SELECT * FROM `khuyenmai` WHERE `id` = '".$id."'";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);            
        $row = $command->queryAll();                        
        return $row;
    }
    
    public function deleteRowById($id)
    {
        $row = array();                        
        $sql = "DELETE FROM `khuyenmai` WHERE `khuyenmai`.`id` = ".$id." ";        
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }
    
    public function countDatesWin($tungay,$denngay){
       // $cache = false;
        $row = array();
        $cache = Yii::app()->cache;
        if($cache != null){
            $cacheService = new CacheService("Khuyenmai","countDatesWin",$tungay.$denngay);
            $key = $cacheService->createKey();
            $dependency = $cacheService->createDependency();
            $cache = Yii::app ()->cache->get ( $key ); 
        }
        $cache=false;
        if($cache == false){ 
            $sql = "SELECT `win_date` FROM `khuyenmai` WHERE `win_date` >= '".$tungay."' AND `win_date` <= '".$denngay."' GROUP BY `win_date`";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll();

            Yii::app ()->cache->set ( $key, $row, ConstantsUtil::TIME_CACHE_3600, new CGlobalStateCacheDependency ( $dependency ) );
        }else{
            $row = $cache;
        }
        return $row;
    }

    public function getRowsByDates($tungay,$denngay){
        // $cache = false;
        $row = array();
        $cache = Yii::app()->cache;
        if($cache != null){
            $cacheService = new CacheService("Khuyenmai","getRowsByDates",$tungay.$denngay);
            $key = $cacheService->createKey();
            $dependency = $cacheService->createDependency();
            $cache = Yii::app ()->cache->get ( $key ); 
        }
         $cache=false;
        if($cache == false){ 
            $sql = "SELECT * FROM `khuyenmai` WHERE `win_date` >= '".$tungay."' AND `win_date` <= '".$denngay."'";                        
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll();

            Yii::app ()->cache->set ( $key, $row, ConstantsUtil::TIME_CACHE_3600, new CGlobalStateCacheDependency ( $dependency ) );
        }else{
            $row = $cache;
        }
        return $row;
    }
    
    public function insertRow($ten,$giai,$diachi,$dienthoai,$date_win){
        $date = date('Y-m-d',time());
        $row = array();                        
        $sql = "INSERT INTO `khuyenmai` (`ten`, `type`, `diachi`, `dienthoai`, `win_date`, `create_date`) VALUES ('".$ten."','".$giai."','".$diachi."','".$dienthoai."','".$date_win."','".$date."');";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    
    public function updateRowById($id,$ten,$giai,$diachi,$dienthoai,$date_win){
        $date = date('Y-m-d',time());
        $row = array();                        
        $sql = "UPDATE `khuyenmai` SET `ten` = '".$ten."', `type` = '".$giai."', `diachi` = '".$diachi."', `dienthoai` = '".$dienthoai."', `win_date` = '".$date_win."' WHERE `id` = ".$id." ";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->execute();            
        return $row;
    }
    
}
